#include <iostream>
#include <string>

#define ADDRESSBOOK_MAXITEM 1000

using namespace std;

struct Member {
    string  name;
    int     phonenumber;
    int     age;
    int     sex;
    string  address;
};

struct AddressBook {
    Member  person[ADDRESSBOOK_MAXITEM];
    int     size;
};

void addMember(AddressBook* example) {
    if (example->size == 1000) {
        cout << "通讯录已满，无法再添加" << endl;
        return;
    }
    cout << "请输入相关内容" << endl;
    cout << "姓名:";
    cin >> example->person[example->size].name;
    cout << "电话号码:";
    cin >> example->person[example->size].phonenumber;
    cout << "年龄:";
    cin >> example->person[example->size].age;
    cout << "性别:";
    cin >> example->person[example->size].sex;
    cout << "地址:";
    cin >> example->person[example->size].address;
    cout << "输入完成" << endl;
    example->size++;
    system("pause");
    system("cls");
}

void showMember(AddressBook *example) {
    if (example->size == 0) {
        cout << "通讯录中无联系人信息" << endl;
    }
    for (int i = 0; i < example->size; i++) {
        cout << "姓名：" << example->person[i].name << " "
             << "电话号码:" << example->person[i].phonenumber << " "
             << "年龄：" << example->person[i].age << " "
             << "性别：" << example->person[i].sex << " "
             << "地址：" << example->person[i].address << " "
             << endl;
    }
    system("pause");
    system("cls");
}

int findItem(AddressBook *example, string name) {
    for (int i = 0; i < example->size; i++) {
        if (name == example->person->name) {
            return i;
            break;
        }
    }

    return -1;  // FIXME: 如果这个函数没有找到，返回-1，那么你下面的程序用返回的数值做下标去取数值，可能导致数组越界
}

void deleteMember(AddressBook *example) {
    cout << "请输入您要删除的联系人:";
    string name;
    cin >> name;
    if (findItem(example, name) == -1) {
        cout << "未找到该联系人" << endl;
    }
    else {
        for (int j = findItem(example, name); j < example->size; j++) {
            example->person[j] = example->person[j + 1];
        }
        example->size--;
    }
    system("pause");
    system("cls");
}

void findMember(AddressBook *example){
    cout << "请输入您要查找的联系人";
    string name;
    cin >> name;
    if (findItem(example, name) == -1) {
        cout << "未找到该联系人" << endl;
    }
    else {
        int j = findItem(example, name);
        cout << "姓名：" << example->person[j].name << " " << "电话号码:" << example->person[j].phonenumber <<
                " " << "年龄：" << example->person[j].age << "性别：" << example->person[j].sex << " " <<
                "地址：" << example->person[j].address << " " << endl;
    }
    system("pause");
    system("cls");
}

void changeMember(AddressBook *example) {
    cout << "请输入您要修改的的联系人姓名";
    string name;
    cin >> name;
    if (findItem(example, name) == -1) {
        cout << "未找到该联系人" << endl;
    }
    else {
        cout << "请输入新的联系人信息" << endl;
        cout << "姓名：";
        string name;
        cin >> name;
        name= example->person[findItem(example, name)].name;
        cout << "年龄：";
        int age;
        cin >> age;
        age= example->person[findItem(example, name)].age;
        cout << "姓别：";
        string sex;
        cin >> sex;
        sex = example->person[findItem(example, name)].sex;
        cout << "电话号码：";
        int phonenumber;
        cin >> phonenumber;
        phonenumber = example->person[findItem(example, name)].phonenumber;
        cout << "地址：";
        string address;
        cin >> address;
        address = example->person[findItem(example, name)].address;
    }
    system("pause");
    system("cls");
}

void clear(AddressBook *example) {
    example->size = 0;
    system("pause");
    system("cls");
}

void Menu() {
    cout << "*****  1.添加联系人  *****" << endl;
    cout << "*****  2.显示联系人  *****" << endl;
    cout << "*****  3.删除联系人  *****" << endl;
    cout << "*****  4.查找联系人  *****" << endl;
    cout << "*****  5.修改联系人  *****" << endl;
    cout << "*****  6.清空联系人  *****" << endl;
    cout << "*****  7.退出通讯录  *****" << endl;
}

int main() {
    int select = 0;
    AddressBook a;
    a.size = 0;

    while (true) {
        Menu();
        cin >> select;
        switch (select) {
        case 1:
            addMember(&a);
            break;
        case 2:
            showMember(&a);
            break;
        case 3:
            deletemember(&a);
            break;
        case 4:
            findmember(&a);
            break;
        case 5:
            changemember(&a);
            break;
        case 6:
            clear(&a);
            break;
        case 7:
            cout << "感谢您的使用" << endl;
            system("pause");
            return 0;
            break;
        default:
            system("pause");
            system("cls");
            break;
        }
    }

    system("pause");
    return 0;
}


// FIXME:
//  1. 写的挺好的！
//  2. 构思、写的时候可以关注程序的结构设计、正确性等
//  3. 写完，测试程序没有问题，可以把代码梳理一下（格式、命名等）。我改了几个名字，你参考我帮你改的，把其他不规范的名字改一下
//  4. 这个程序当然功能已经实现了，考虑如何用C++的方式来写这个程序
//  5. 例如，如何把 `addMember`等作为`AddressBook`类的成员函数
//  6. 可以使用 [GitKraken](https://www.gitkraken.com) 对比查看修改的地方
